var moduleIddata= getQueryString('moduleId');//专题id
var mbId= getQueryString('mbId');//模板id
console.log(moduleIddata,mbId)
var seavedata=[];//保存传给后台的值
var initdataList=[];//初始化需要就调单类型模板详情查询 接口做回显
//如果有moduleIddata 就调单类型模板详情查询 接口做回显
var pos =  getQueryString('pos');//在添加模板页 添加的模板的位置顺序
var modelIdeveyList=[];
var modelIdevey = {'typeId':1,'id':''};//需要传给 商品下载集合
var updateModeId = '';//编辑进来的 点击保存要添加的入参
initdata (mbId)
var imghtml='';
var liwidth = '';
function initdata (mbId){
    if(mbId){
        $(".change").html('模块编辑')
        web.doAjax(root.special.searchModelMsg, 'post', {'modelArrayId':mbId,'subId':moduleIddata}, succCallBack)
    }
}
function succCallBack(data){
    if(data.errorCode == 0){
        //页面回显
        if(data.data != null && data.data.length >0){
         $.each(data.data,function (i,ite) {
             console.log('这是索引')
             console.log(i)
             //图片渲染
             imghtml += '<li><img src=" '+$(ite)[0].modelUrl+' "><span class="name">'+$(ite)[0].productTitle+'</span><span class="price" style="display:none;">￥'+$(ite)[0].productPrice+' </span></li>'
                 $(".radionamebox").find('input').each(function(j,item){
                         if($(item).val() ==$(ite)[0].modelStatus){
                             $(item).attr('checked','checked')  
                         }
                     })
                     $(".radioretivalbox").find('input').each(function(j,item){
                         if($(item).val() == $(ite)[0].modelSpace){
                             $(item).attr('checked','checked')  
                         }
                     })
         if($(ite)[0].placeType == 4){
             var dex = i+1
             if(dex >8){
                 $(".spubox").append('<div class="clearfix mt-15 spuli sortspu"><label class="form-label f-l"><span class="c-red pr-5">*</span> SPU码 <span class="sortli">'+dex+'</span>：</label><div class="formControls f-l">'+
                     '<input   autocomplete="off" name="picTitle'+dex+'" type="text" class="input-text input-text-spu radius" placeholder="" maxlength="25"><a onclick="deletespu(this,'+dex+')" href="javascript:;">删除</a></div></div>')
            
             }
             
                $(".spubox").find('input').each(function(sp,splst){
                    if(sp == i){
                        $(splst).val($(ite)[0].placeContent)
                    }   
                })
         } 
         updateModeId = $(ite)[0].modelId;
         modelIdevey= {'typeId':1,'id': $(ite)[0].modelId};
         modelIdeveyList.push(modelIdevey)    
         })
         $(".img").html(imghtml)
         liwidth = $(".spubox").find('.spuli').length * 94;
         $(".img").css({'width':liwidth})
        }
     }else{
        $.Huimodalalert('失败！',2000);
     }
}
var dataurl ='';
$("#form-member").validate({
    rules: {
        picTitle:{
            required: true,//是否是必填项
        },
        picTitle2:{
            required: true,//是否是必填项
        },
        picTitle3:{
            required: true,//是否是必填项
        },
        picTitle4:{
            required: true,//是否是必填项
        },
        picTitle5:{
            required: true,//是否是必填项
        },
        picTitle6:{
            required: true,//是否是必填项
        },
        picTitle7:{
            required: true,//是否是必填项
        },
        picTitle8:{
            required: true,//是否是必填项
        },
        picTitle9:{
            required: true,//是否是必填项
        },
        picTitle10:{
            required: true,//是否是必填项
        },
        picTitle11:{
            required: true,//是否是必填项
        },
        picTitle12:{
            required: true,//是否是必填项
        },
        picTitle13:{
            required: true,//是否是必填项
        },
        picTitle14:{
            required: true,//是否是必填项
        },
        picTitle15:{
            required: true,//是否是必填项
        }
    },
    //提交表单数据
submitHandler:function(form){
seavedata=[];
spulieveryList=[];
checksform();
var params = {
                "subId":moduleIddata,//专题页id
                "modelUrl":'',//图片链接
                "picMsg":'',//图片信息(高:picHeight 宽 : picWidth)
                "modelType":'6',//模板类型(1.1张图2.2张图3.3张图纵向4.3张图横向5.单品详情6.商品集合)
                "contentIndex":'1',//图片排序
                "modelStatus":$(".radionamebox").find('input:checked').val(),//模板状态(是否展示):展示0:不展示1
                "modelSpace":$(".radioretivalbox").find('input:checked').val(),//模板间隔1:间隔0:不间隔
                "placeType":'4',//插入链接类型(插入链接类型 1:专题页 2.分类页 3.品牌页 4.单品详情页 5.自定义url 6.商品集合 7.优惠券 8.无跳转)
                "placeContent":spulieveryList.join(),//链接内容
                "detailContent" :'',
                "userName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
}
if(mbId){
    dataurl = root.special.updateModel;
    params.modelId = updateModeId
    }else{
     dataurl = root.special.addAppModel
     }
seavedata.push(params)
        $.ajax({
            url:dataurl,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify(seavedata)  ,
            success: function (data) {
                if(data.errorCode == 0){
                    if(mbId){
                        parent.specialEdit.singleModuleDagaGet(mbId,pos)
                    }else{
                        parent.specialEdit.singleModuleDagaGet(data.data,pos)
                    }
                    layer.msg("保存成功",{icon: 6,time:1000},function () {
                        setTimeout(function () {
                          var index = parent.layer.getFrameIndex(window.name);
                          parent.layer.close(index);
                      })
                  });
       
                }else if(data.errorCode == 5){
                    $.Huimodalalert(data.msg,2000);
                 
                 }else{
                    $.Huimodalalert(data.msg,2000);
                   
                 }  
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
    }
})
function closefn(){
    var curInd = layer.confirm(
    '<span>确认取消已编辑的模块</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']},
     function(index){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);

    },function(){
        layer.close(curInd);
    });
}
var addnum = 8;
function addspuiptfn(){
    var addlastspuli= $(".sortspu:last").find('.sortli:last').html();
if(addlastspuli >8){
   if(addlastspuli <15){
    sortfn();
    addlastspuli ++
    deltaddsortfn(addlastspuli)
    if(addlastspuli == 15){
       $("#addspuipt").css('display','none');
     }
   }else if(addlastspuli == 15){
    deltaddsortfn(addlastspuli)
     $("#addspuipt").css('display','none');
   }else{
    $("#addspuipt").css('display','none');
   }
}else{
    addnum ++
    if(addnum < 15){
        addsortfn(addnum)
        $("#addspuipt").css('display','block');
                              
 }else if(addnum == 15){
    addsortfn(addnum)
    $("#addspuipt").css('display','none');
}else{
    $("#addspuipt").css('display','none');
}
}
}
function deletespu(e,denum){
    $(e).parent().parent().remove()
    $("#addspuipt").css('display','block');
    sortfn()
}
//删除排序
function sortfn(){
    var j= 8;
    $(".sortspu").find('.sortli').each(function (i,item) { 
        j ++ 
      $(item).html(j)
     })
}
//s删除之后再次新增 排序
function deltaddsortfn(indexspu){
    
    $(".spubox").append('<div class="clearfix mt-15 spuli sortspu"><label class="form-label f-l"><span class="c-red pr-5">*</span> SPU码 <span class="sortli">'+indexspu+'</span>：</label><div class="formControls f-l">'+
                                '<input autocomplete="off" name="picTitle'+indexspu+'" maxlength="25" onkeyup = "isnumfn(this)" type="text" class="input-text input-text-spu radius" placeholder="请输入SPU"><a onclick="deletespu(this,'+indexspu+')" href="javascript:;">删除</a></div></div>')
  
}
//新增排序
function addsortfn(addspuindex){
    $(".spubox").append('<div class="clearfix mt-15 spuli sortspu"><label class="form-label f-l"><span class="c-red pr-5">*</span> SPU码 <span class="sortli">'+addspuindex+'</span>：</label><div class="formControls f-l">'+
                                '<input autocomplete="off" name="picTitle'+addspuindex+'" maxlength="25"  onkeyup = "isnumfn(this)"  type="text" class="input-text input-text-spu radius" placeholder="请输入SPU"><a onclick="deletespu(this,'+addspuindex+')" href="javascript:;">删除</a></div></div>')
}

//提交多个spu存储
var spulievery = '';//填入的每一个spu
var spulieveryList= [];//存储填入的每一个spu
var errdata = ['a','c']
function checksform(){
    
    $(".spubox").find('.spuli').each(function(i,item){
        if(!$(item).find('input').val()){
       }else{
        spulievery = $(item).find('input').val()
       }
       spulieveryList.push(spulievery)

    })
return  spulieveryList
}

function isnumfn(e){
    $(e).val($(e).val().replace(/[^\d]/g, ''));
}

